package org.jeecg.modules.veterans.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.util.SecurityUtil;
import org.jeecg.modules.veterans.entity.SysLikeRecord;
import org.jeecg.modules.veterans.mapper.SysLikeRecordMapper;
import org.jeecg.modules.veterans.service.ISysLikeRecordService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 点赞记录表
 * @Author: jeecg-boot
 * @Date:   2022-07-06
 * @Version: V1.0
 */
@Api(tags="点赞记录表")
@RestController
@RequestMapping("/veterans/sysLikeRecord")
@Slf4j
public class SysLikeRecordController extends JeecgController<SysLikeRecord, ISysLikeRecordService> {
	@Autowired
	private ISysLikeRecordService sysLikeRecordService;
	
	/**
	 * 分页列表查询
	 *
	 * @param sysLikeRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "点赞记录表-分页列表查询")
	@ApiOperation(value="点赞记录表-分页列表查询", notes="点赞记录表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<SysLikeRecord>> queryPageList(SysLikeRecord sysLikeRecord,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysLikeRecord> queryWrapper = QueryGenerator.initQueryWrapper(sysLikeRecord, req.getParameterMap());
		Page<SysLikeRecord> page = new Page<SysLikeRecord>(pageNo, pageSize);
		IPage<SysLikeRecord> pageList = sysLikeRecordService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 获取业务浏览总数
	  *
	  * @param recordId
	  * * @param type
	  * @return
	  */
	 //@AutoLog(value = "浏览记录信息表-获取业务浏览总数")
	 @ApiOperation(value="浏览记录信息表-获取业务浏览总数", notes="浏览记录信息表-获取业务浏览总数")
	 @GetMapping(value = "/total")
	 public Result<String> total(@RequestParam(name = "recordId", required = true) String recordId,@RequestParam(name = "type", required = true) String type) {
		 String total = "0";
		 QueryWrapper<SysLikeRecord> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("type",type);
		 queryWrapper.eq("record_id",recordId);
		 queryWrapper.eq("liked","1");
		 List<SysLikeRecord> list = sysLikeRecordService.list(queryWrapper);
		 if(!list.isEmpty()){
			 total = String.valueOf(list.size());
		 }

		 return Result.OK(total);
	 }

	 /**
	  * 通过id查询Likeflag
	  *
	  * @param recordId
	  * @return
	  */
	 //@AutoLog(value = "点赞记录表-通过id查询Likeflag")
	 @ApiOperation(value="点赞记录表-通过id查询Likeflag", notes="点赞记录表-通过id查询Likeflag")
	 @GetMapping(value = "/likeflag")
	 public Result<String> likeflag(@RequestParam(name = "recordId", required = true) String recordId,@RequestParam(name = "type", required = true) String type) {
		 String total = "0";
		 LoginUser loginUser = SecurityUtil.getLoginUser();
		 QueryWrapper<SysLikeRecord> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("type",type);
		 queryWrapper.eq("record_id",recordId);
		 queryWrapper.eq("user_id",loginUser.getId());
		 List<SysLikeRecord> list = sysLikeRecordService.list(queryWrapper);
		 if(!list.isEmpty()){
			 total = String.valueOf(list.size());
		 }

		 return Result.OK(total);
	 }


	 /**
	  *   添加
	  *
	  * @param sysLikeRecord
	  * @return
	  */
	 @AutoLog(value = "点赞记录表-添加")
	 @ApiOperation(value="点赞记录表-添加", notes="点赞记录表-添加")
	 @PostMapping(value = "/add")
	 public Result<String> add(@RequestBody SysLikeRecord sysLikeRecord) {
		 sysLikeRecordService.saveByOne(sysLikeRecord);
		 return Result.OK("添加成功！");
	 }

	/**
	 *  编辑
	 *
	 * @param sysLikeRecord
	 * @return
	 */
	@AutoLog(value = "点赞记录表-编辑")
	@ApiOperation(value="点赞记录表-编辑", notes="点赞记录表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody SysLikeRecord sysLikeRecord) {
		sysLikeRecordService.updateById(sysLikeRecord);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param recordId
	 * @return
	 */
	@AutoLog(value = "点赞记录表-通过id删除")
	@ApiOperation(value="点赞记录表-通过id删除", notes="点赞记录表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="recordId",required=true) String recordId) {
		LoginUser loginUser = SecurityUtil.getLoginUser();
		Map<String,Object> map = new HashMap<>();
		map.put("record_id",recordId);
		map.put("user_id",loginUser.getId());
		sysLikeRecordService.removeByMap(map);
//		sysLikeRecordService.removeById(recordId);

		return Result.OK("删除成功!");
	}

	/**7
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "点赞记录表-批量删除")
	@ApiOperation(value="点赞记录表-批量删除", notes="点赞记录表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysLikeRecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "点赞记录表-通过id查询")
	@ApiOperation(value="点赞记录表-通过id查询", notes="点赞记录表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<SysLikeRecord> queryById(@RequestParam(name="id",required=true) String id) {
		SysLikeRecord sysLikeRecord = sysLikeRecordService.getById(id);
		if(sysLikeRecord==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(sysLikeRecord);
	}


    /**
    * 导出excel
    *
    * @param request
    * @param sysLikeRecord
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysLikeRecord sysLikeRecord) {
        return super.exportXls(request, sysLikeRecord, SysLikeRecord.class, "点赞记录表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysLikeRecord.class);
    }

}
